//
//  SecurityHelperVC.m
//  PrivateSteward
//
//  Created by lanou3g on 16/4/1.
//  Copyright © 2016年 闫贺晨. All rights reserved.
//

#import "SecurityHelperVC.h"
#import "SecurityHelperCell.h"
#import "SecurityDetailVC.h"

@interface SecurityHelperVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,assign)NSInteger numberOfRows;
@property(nonatomic,strong)SecurityDetailVC *securityDetilvc;
@end

@implementation SecurityHelperVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.securityTB registerClass:[SecurityHelperCell class] forCellReuseIdentifier:@"security"];
    self.securityTB.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.securityTB.delegate = self;
    self.securityTB.dataSource = self;
    [self.view addSubview:self.securityTB];
    self.securityDetilvc = [[SecurityDetailVC alloc]init];
    
    
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationItem.title = @"登陆";
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Add)];
}
-(void)Add
{
    self.securityDetilvc.isAdd = YES;
}
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.numberOfRows > 0) {
        self.securityDetilvc.isAdd = NO;
        [self.navigationController pushViewController:self.securityDetilvc animated:YES];
        AVQuery *query = [AVQuery queryWithClassName:@"securityHelper"];
        [query whereKey:@"phoneNumber" equalTo:[[AVUser currentUser] username]];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            self.securityDetilvc.pwdName = objects[indexPath.row][@"localData"][@"pwdName"];
            self.securityDetilvc.pwd = objects[indexPath.row][@"localData"][@"pwd"];
        }];
    }
    else
    {
        self.securityDetilvc.isAdd = YES;
    }
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SecurityHelperCell *cell = [tableView dequeueReusableCellWithIdentifier:@"security"];
    if (cell == nil) {
        cell = [[SecurityHelperCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"security"];
    }
    AVQuery *query = [AVQuery queryWithClassName:@"securityHelper"];
    [query whereKey:@"phoneNumber" equalTo:[[AVUser currentUser] username]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.numberOfRows = objects.count ;
        if (objects.count <= 0) {
            cell.securityNameLable.text = @"对不起,您现在没有任何密码记录";
        }
        else
        {
            cell.securityNameLable.text = objects[indexPath.row][@"localData"][@"pwdName"];
        }
        
    }];
    return cell;
}








-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.numberOfRows == 0) {
        return 1;
    }
    else
    {
        return self.numberOfRows;
    }
}






































- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
